using System;
using System.Collections.Generic;
using System.Text;
using PH.Common;

namespace Hoshimi
{
    public class CollectorBot_MoveToHoshimiState : State
	{
		#region Singleton

		// Fields
		protected static CollectorBot_MoveToHoshimiState instance;

		// Constructor
		protected CollectorBot_MoveToHoshimiState() : base() { }

		// Methods
		public static CollectorBot_MoveToHoshimiState Instance()
		{
			// Uses "Lazy initialization"
			if( instance == null ) 
				instance = new CollectorBot_MoveToHoshimiState();

			return instance;
		}

		#endregion

		public override bool Play( Unit unit ) 
        {
			CollectorBot bot = (CollectorBot) unit;
			
			bot.SetName("MoveToHoshimi");

            if (bot.IsEmpty())
            {
                bot.StopIfMoving();
                unit.myStateMachine.ChangeState( CollectorBot_MoveToAZNState.Instance() );
				return true;
            }

			// The needle is full or dead ? We stop
			if (	bot.IsMyNeedleTakenByEnemy()	|| 
					bot.IsMyNeedleFullOrDead()		|| 
					!bot.IsNeedlePointAssigned()) 
			{
				bot.StopIfMoving();
				bot.MoveToNanoAI();
				return true;
			}

			// We cant give order to the bot. We stop
			if ( bot.State != NanoBotState.WaitingOrders ) 
			{
				return true;
			}

			// We are not on the Needle point
			if ( !bot.IsTransferPossible() ) 
			{
				if (!bot.MoveToNeedle())
				{
					bot.MoveToNanoAI();
				}
				return true;
			}	

			// The needle isn't already built
			if ( !bot.IsMyNeedleAssigned() )
			{
				return true;
			}

			// The bot is on the Needle point : we order to transfer
			unit.myStateMachine.ChangeState( CollectorBot_TransferAZNState.Instance() );
			return false;
		}
	}
}
